using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace Avinor.NET
{
    public class AvinorWebClient
    {
        public IEnumerable<Flight> GetFlights(FlightsQuery flightsQuery)
        {
            using (Stream data = GetStream("http://flydata.avinor.no/XmlFeed.asp?" + flightsQuery.Query))
            {
                var parser = new AvinorFlightParser(data);
                return parser.GetFlights();
            }
        }

        public IEnumerable<Airport> GetAirports()
        {
            using (Stream data = GetStream("http://flydata.avinor.no/airportNames.asp"))
            {
                var parser = new AvinorAirportParser(data);
                return parser.GetAirports();
            }
        }

        Stream GetStream(string uri)
        {
            WebClient client = new WebClient();
            client.Encoding = Encoding.GetEncoding("ISO-8859-1");

            return client.OpenRead(uri);
        }
    }
}